package han.temp;

import com.han.http.client.exception.NullContentMatchedException;
import com.han.http.util.Regex;
import han.entity.InvestCompanyInfoEntity;
import han.fetch.InfoSupplementer;
import han.sql.Operator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import static com.han.http.util.Print.printlnErr;

/**
 * @author: Han
 * @date: 2018/9/19 8:49
 * @dscription: 将以前抓到的联系信息和现有的整合
 */
public class InvestInfoFilter {
    public static void main(String... args) {
        Operator operator = Operator.getInstance();
        List<InvestCompanyInfoEntity> newInvestInfoList = operator.queryAllCompanyInfo();
        List<InvestCompanyInfoEntity> oldInvestInfoList = operator.queryAllTempCompanyInfo();
        Map<String, InvestCompanyInfoEntity> oldMap = generateMap(oldInvestInfoList);
        Random random = new Random();
        for (InvestCompanyInfoEntity newEntity : newInvestInfoList) {
            String id = newEntity.getKrId();
            if (oldMap.containsKey(id)) {
                InvestCompanyInfoEntity oldEntity = oldMap.get(id);
                String email = oldEntity.getEmail();
                String address = oldEntity.getAddress();
                String phone = oldEntity.getPhoneNumber();
                newEntity.setEmail(email);
                newEntity.setAddress(address);
                newEntity.setPhoneNumber(phone);
            } else {
                String name = newEntity.getFullName();
                name = ("-".equals(name) || "".equals(name)) ? newEntity.getName() : name;
                //如果名称中包含5个以上英文字符,则不查询此公司
                try {
                    Regex.matchString(name, "[a-zA-Z0-9\\s.'_+/*~!]{5,1000}");
                    continue;
                } catch (NullContentMatchedException ignore) {
                }
                try {
                    String[] contact = InfoSupplementer.qiChaCha(name, "PHPSESSID=ejnhu0to2sea4eqjk8fch03fs6; UM_distinctid=165d71a97ea961-07b1e9eed1d85f-6154702a-1fa400-165d71a97eca11; _uab_collina=153691216965876673017412; acw_tc=b461f59b15369121680836554eb2823728e3d92f47ea34f0c252ebbdb4; _umdata=70CF403AFFD707DFCE60A8EB03D5914878164B3F0A090A35329EA7B475C3A22A90C4E59E30B0C4A2CD43AD3E795C914C8CA6FE223604BDFDD61C457D4F586AFC; CNZZDATA1254842228=130082512-1536910380-https%253A%252F%252Fwww.baidu.com%252F%7C1537317755; hasShow=1; Hm_lvt_3456bee468c83cc63fb5147f119f1075=1537145202,1537154495,1537178698,1537319603; Hm_lpvt_3456bee468c83cc63fb5147f119f1075=1537319746");
                    newEntity.setEmail(contact[0]);
                    newEntity.setPhoneNumber(contact[1]);
                    newEntity.setAddress(contact[2]);
                } catch (Exception e) {
                    printlnErr("获取:" + name + "联系信息失败");
                    e.printStackTrace();
                } finally {
                    try {
                        int interval = random.nextInt(1000 * 10) + 1000 * 5;
                        Thread.sleep(interval);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            operator.updateCompanyInfo(newEntity);
        }
    }

    private static Map<String, InvestCompanyInfoEntity> generateMap(List<InvestCompanyInfoEntity> list) {
        Map<String, InvestCompanyInfoEntity> map = new HashMap<>();
        for (InvestCompanyInfoEntity entity : list) {
            String id = entity.getKrId();
            map.put(id, entity);
        }
        return map;
    }
}
